﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DbMapping.Common;
using VideoCache.Server.Model;
using VideoCache.Server.BLL;
using VideoCache.Server.MVCWeb.Attributes;
using VideoCache.Server.MVCWeb.Areas.Admin.Models;
using VideoCache.Server.MVCWeb.Areas.Member.Models;

namespace VideoCache.Server.MVCWeb.Areas.Admin.Controllers
{
    [ActionAuthorize(AuthorizeType.Administrator)]
    public class NetbarController : Controller
    {
        //
        // GET: /Admin/Netbar/
        public ActionResult Index(string keyword, int page = 1)
        {
            Conditions cons = new Conditions();
            ViewBag.Keyword = keyword;

            Orders orders = new Orders();
            orders.Add(new OrderItem(NetbarInfo.CreateTimeField, OrderDirection.Desc));

            Conditions conditions = new Conditions();

            if (!string.IsNullOrEmpty(keyword)) conditions.Add(new ConditionItem(LinkType.And, AlbumInfo.TitleField, CompareType.StartWith, keyword));

            int recordCount = 0;
            int pageSize = 30;

            var list = NetbarBLL.FindPage(conditions, orders, page, pageSize, ref recordCount);

            ViewBag.PageSize = pageSize;
            ViewBag.RecordCount = recordCount;

            return View(list);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(CreateNetbarModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    NetbarInfo info = new NetbarInfo();
                    info.Name = model.Name;
                    info.Password = model.Password;
                    info.Province = model.Province;
                    info.City = model.City;
                    info.District = model.District;
                    info.ContactPerson = model.ContactPerson;
                    info.Telephone = model.Telephone;
                    info.MobilePhone = model.MobilePhone;
                    info.CreateTime = DateTime.Now;
                    info.ModifyTime = DateTime.Now;
                    info.LastAuthTime = DateTime.Now;
                    info.LastRequestTime = DateTime.Now;
                    info.ExpiredTime = DateTime.Now;

                    NetbarBLL.Add(info);

                    return RedirectToAction("Index");
                }
                catch
                {
                    ModelState.AddModelError("Error", "保存失败，请稍后重试。");
                }
            }

            return View(model);
        }

        public ActionResult Edit(int id)
        {
            NetbarInfo info = NetbarBLL.Find(id);

            if (info == null) return HttpNotFound();

            CreateNetbarModel model = new CreateNetbarModel();
            model.Id = info.Id;
            model.Name = info.Name;
            model.Province = info.Province;
            model.City = info.City;
            model.District = info.District;
            model.ContactPerson = info.ContactPerson;
            model.Telephone = info.Telephone;
            model.MobilePhone = info.MobilePhone;

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(CreateNetbarModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    NetbarInfo info = NetbarBLL.Find(model.Id);
                    if (info != null)
                    {
                        info.Name = model.Name;
                        info.Password = model.Password;
                        info.Province = model.Province;
                        info.City = model.City;
                        info.District = model.District;
                        info.ContactPerson = model.ContactPerson;
                        info.Telephone = model.Telephone;
                        info.MobilePhone = model.MobilePhone;
                        info.CreateTime = DateTime.Now;
                        info.ModifyTime = DateTime.Now;
                        info.LastAuthTime = DateTime.Now;
                        info.LastRequestTime = DateTime.Now;
                        info.ExpiredTime = DateTime.Now;

                        NetbarBLL.Add(info);

                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ModelState.AddModelError("Error", "要修改的网吧不存在或已删除。");
                    }
                }
                catch
                {
                    ModelState.AddModelError("Error", "保存失败，请稍后重试。");
                }
            }

            return View(model);
        }

        public ActionResult Delete(int id)
        {
            NetbarBLL.Delete(id);

            return RedirectToAction("Index");
        }

        public ActionResult Details(int id)
        {
            NetbarInfo info = NetbarBLL.Find(id);

            if (info == null) return HttpNotFound();

            return View(info);
        }

        public ActionResult Pay(int id)
        {
            NetbarInfo info = NetbarBLL.Find(id);
            if (info == null) return HttpNotFound();

            return View(info);
        }

        [HttpPost]
        public ActionResult Pay(PayModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    NetbarInfo info = NetbarBLL.Find(model.Id);

                    if (info == null) return HttpNotFound();

                    info.ExpiredTime = model.ExpiredTime;

                    NetbarBLL.Update(info);

                    return RedirectToAction("Index");
                }
                catch
                {
                    ModelState.AddModelError("Error", "充值失败，请稍后重试。");
                }
            }

            return View(model);
        }
    }
}
